package com.woniuxy.mqConsumer.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.woniuxy.common.enums.RabbitEnum.*;
import static com.woniuxy.common.enums.RabbitEnum.ORDER_DEAD_ROUTING_KEY;

@Configuration
public class RabbitMqConfig {
    /*@Bean
    public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        //设置开启Mandatory,才能触发回调函数,无论消息推送结果怎么样都强制调用回调函数
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());//配置RabbitMQ消息转换器，可以传输对象
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("ConfirmCallback:     "+"相关数据："+correlationData);
                System.out.println("ConfirmCallback:     "+"确认情况："+ack);
                System.out.println("ConfirmCallback:     "+"原因："+cause);
            }
        });

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                System.out.println("ReturnCallback:     "+"消息："+message);
                System.out.println("ReturnCallback:     "+"回应码："+replyCode);
                System.out.println("ReturnCallback:     "+"回应信息："+replyText);
                System.out.println("ReturnCallback:     "+"交换机："+exchange);
                System.out.println("ReturnCallback:     "+"路由键："+routingKey);
            }
        });
        return rabbitTemplate;
    }*/
    @Bean
    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }


    //正常队列
    public static final String NORMAL_QUEUE = "NORMAL_QUEUE";
    //正常交换机
    public static final String NORMAL_EXCHANGE = "NORMAL_EXCHANGE";
    //死信交换机
    public static final String DEAD_EXCHANGE = "DEAD_EXCHANGE";
    //死信队列
    public static final String DEAD_QUEUE = "DEAD_QUEUE";
    //正常队列路由
    public static final String NORMAL_ROUTING_KEY = "NORMAL_ROUTING_KEY";
    //死信队列路由
    public static final String DEAD_ROUTING_KEY = "DEAD_ROUTING_KEY";

    @Bean
    public DirectExchange normalExchange(){
        return new DirectExchange(NORMAL_EXCHANGE);
    }
    @Bean
    public DirectExchange deadExchange(){
        return new DirectExchange(DEAD_EXCHANGE);
    }
    @Bean
    public Queue deadQueque(){
        return new Queue(DEAD_QUEUE);
    }
    //声明正常队列
    @Bean
    public Queue normalQueue(){
        Map<String,Object> arguments = new HashMap<>(2);
        //绑定该队列到死信交换机
        arguments.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        arguments.put("x-dead-letter-routing-key",DEAD_ROUTING_KEY);
        return new Queue(NORMAL_QUEUE,true,false,false,arguments);
    }
    @Bean
    public Binding normalBinding(){
        return BindingBuilder.bind(normalQueue()).to(normalExchange()).with(NORMAL_ROUTING_KEY);
    }
    @Bean Binding deadBinding(){
        return BindingBuilder.bind(deadQueque()).to(deadExchange()).with(DEAD_ROUTING_KEY);
    }

    @Bean
    public RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate();
        rabbitTemplate.setConnectionFactory(connectionFactory);
        //设置开启Mandatory,才能触发回调函数,无论消息推送结果怎么样都强制调用回调函数
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());//配置RabbitMQ消息转换器，可以传输对象
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                System.out.println("ConfirmCallback:     "+"相关数据："+correlationData);
                System.out.println("ConfirmCallback:     "+"确认情况："+ack);
                System.out.println("ConfirmCallback:     "+"原因："+cause);
            }
        });

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
                System.out.println("ReturnCallback:     "+"消息："+message);
                System.out.println("ReturnCallback:     "+"回应码："+replyCode);
                System.out.println("ReturnCallback:     "+"回应信息："+replyText);
                System.out.println("ReturnCallback:     "+"交换机："+exchange);
                System.out.println("ReturnCallback:     "+"路由键："+routingKey);
            }
        });
        return rabbitTemplate;
    }

    /**
     * 订单队列+交换机
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange(ORDER_NORMAL_EXCHANGE);
    }
    //创建死信交换机
    @Bean
    public DirectExchange deadOrderExchange(){
        return new DirectExchange(ORDER_DEAD_EXCHANGE);
    }

    //创建正常队列 （重要）
    @Bean
    public Queue queueNormal(){
        return QueueBuilder.durable(ORDER_NORMAL_QUEUE)
                .deadLetterExchange(ORDER_DEAD_EXCHANGE)
                .deadLetterRoutingKey(ORDER_DEAD_ROUTING_KEY)
                .ttl(1*60*1000)
                .build();
    }

    //创建死信队列
    @Bean
    public Queue deadQueue(){
        return QueueBuilder.durable(ORDER_DEAD_QUEUE).build();
    }

    //正常交换机绑定正常队列
    @Bean
    public Binding bindDirectExchangeAndQueueA(){
        return BindingBuilder.bind(queueNormal()).to(directExchange()).with(ORDER_NORMAL_ROUTING_KEY);
    }
    //死信交换机绑定死信队列
    @Bean
    public Binding bindDeadExchangeAndDeadQueue(){
        return BindingBuilder.bind(deadQueue()).to(deadOrderExchange()).with(ORDER_DEAD_ROUTING_KEY);
    }
}
